package com.adam.controller;

import com.adam.pojo.Books;
import com.adam.service.BookService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;
    @RequestMapping("/allBook")
    public String list(Model model){
        List<Books> books = bookService.queryAll();
        model.addAttribute("list",books);
        return "allBook";
    }
    @RequestMapping("/toAddBook")
    public String toBookAdd(){
        return "addBook";
    }
    @RequestMapping("/addBook")
    public String addBook(Books books){
        bookService.addBook(books);
        return "redirect:/book/allBook";
    }
    @RequestMapping("/toUpdateBook")
    public String toUpdateBook(Model model , int id){
        Books books = bookService.queryByID(id);
        model.addAttribute("book",books);
        return "updateBook";
    }
    @RequestMapping("/updateBook")
    public String updateBook(Books books){
        bookService.updateBook(books);
        return "redirect:/book/allBook";
    }
    @RequestMapping("/del/{bookID}")
    public String deleteBook(@PathVariable("bookID") int id){
        bookService.deleteBook(id);
        return "redirect:/book/allBook";
    }
    @RequestMapping("/searchBook")
    public String searchBook(String bookName , Model model){
        List<Books> books = bookService.searchBook(bookName);
        model.addAttribute("list",books);
        return "allBook";
    }
}
